<?php

class base_vcode {

    var $use_gd = true;

    function __construct($type) {
        if ($this->use_gd) {
            $this->obj = kernel::single('base_vcode_gd');
        } else {
            $this->obj = kernel::single('base_vcode_gif');
        }
        $sessionLib = kernel::single('base_session');
        if ($type == 'admin') {
            $sessionLib->is_back_sess_key();
        }
        $sessionLib->start();
    }

    function length($len) {
        $this->obj->length($len);
        return true;
    }

    function verify_key($key) {
        $sess_id = kernel::single('base_session')->sess_id();
        $key = $key . $sess_id;
        $ttl = 180;
        if (defined('WITHOUT_CACHE') && ! constant('WITHOUT_CACHE')) {
            cacheobject::set($key, $this->obj->get_code(), $ttl + time());
        } else {
            base_kvstore::instance('vcode')->store($key, $this->obj->get_code(), $ttl);
        }
    }

    static function verify($key, $value) {
        $value = strtolower($value);
        $sess_id = kernel::single('base_session')->sess_id();
        $key = $key . $sess_id;
        if (defined('WITHOUT_CACHE') && ! constant('WITHOUT_CACHE')) {
            cacheobject::get($key, $vcode);
        } else {
            base_kvstore::instance('vcode')->fetch($key, $vcode);
        }
        if (empty($value)) {
            return false;
        }
        if ($vcode == strval($value)) {
            return true;
        }
        
        return false;
    }

    function display() {
        $this->obj->display();
    }
}
